package _06内存流;

import java.io.*;
import java.util.Arrays;

public class ByteArrayStreamDemo {
    public static void main(String[] args) throws IOException {
        // 了解
        // 字节内存流
        // 字符内存流

        // 用流的方式操作 字节数组 和 字符串

        // 字节数组
//		testWrite();

//		testRead();

        // 字符串
//		testWrite2();

        testRead2();
    }


    private static void testRead2() throws IOException {
        // TODO Auto-generated method stub

        // 1. 创建流
        StringReader sr = new StringReader("a,你好,123123123123");

        // 2. 读取
        System.out.println(sr.read());

        char[] buf = new char[20];
        int len = sr.read(buf);
        System.out.println(Arrays.toString(buf));


        // 3. 不需要关闭流
    }


    private static void testWrite2() {

        // 1. 创建流
        StringWriter sw = new StringWriter();

        // 2.
        sw.write("Hello");
        sw.write(" ");
        sw.write("World!");

        // 3.
        System.out.println(sw.toString());


        // 4. 不需要关闭
//		sw.close();

    }

    private static void testRead() throws IOException {
        // TODO Auto-generated method stub

        // 1.
        byte[] bytes = {10, 20, 30};
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);

        // 2.
        System.out.println(in.read());
        System.out.println(in.read());
        System.out.println(in.read());
        System.out.println(in.read());
        System.out.println(in.read());
        // 3.
        in.close();

    }

    private static void testWrite() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        out.write(49);
        out.write(50);
        out.write(51);
        out.write(52);

        // 3. 得到一个字节数组
        byte[] bytes = out.toByteArray();
        System.out.println(Arrays.toString(bytes));

    }
}